package Orm.mybatis.exa01;

import Orm.jdbc.connectorPool.JDBCUtils;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;

/**
 * mybatis基本使用
 */
public class CreateSqlSessionFactory {
    public static void main(String[] args) {
        //获取数据源连接池
        DataSource dataSource = JDBCUtils.dataSource;
        //获取jdbc事务工厂
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        //创建环境对象
        Environment environment = new Environment("development", transactionFactory, dataSource);
        //根据环境对象创建配置对象
        Configuration configuration = new Configuration(environment);
        //指定要扫描的包
        configuration.addMappers("Orm.mybatis.exa01");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CityQuery cityQuery = sqlSession.getMapper(CityQuery.class);
        City city = cityQuery.selectTest();
        System.out.println(city.toString());

    }
}
